package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.query.album.CategoryList;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;

    @Autowired
    private BaseAttributeValueMapper baseAttributeValueMapper;


    /**
     * 查询所有分类（1、2、3级分类）
     *
     * @return
     */
    @Override
    public List<CategoryList> getBaseCategoryList() {


        // 查询建立视图的所有数据
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);

        // 使用Stream API按categoryId分组  按照一级分类id分组 分组后是一个map
        Map<Long, List<BaseCategoryView>> groupedByCategory1Id = baseCategoryViews.stream()
                .collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        // 创建返回分类列表
        List<CategoryList> categoryLists = new ArrayList<>();

        // 遍历map，将数据封装成分类列表，先判空一下
        if (CollectionUtil.isEmpty(groupedByCategory1Id)) {
            throw new RuntimeException("查询分类失败");
        }
        for (Map.Entry<Long, List<BaseCategoryView>> entry : groupedByCategory1Id.entrySet()) {
            CategoryList categoryList = new CategoryList();
            // 获取键，这里是以一级分类id分组，所以 key就是一级分类id
            Long category1Id = entry.getKey();
            // 获取一级分类名称
            String category1Name = baseCategory1Mapper.selectById(category1Id).getName();
            List<BaseCategoryView> value = entry.getValue();// 获取值
            List<CategoryList> categoryList1 = new ArrayList<>();
            // 处理二级分类，同理，将二级分类id分组，并封装成分类列表
            Map<Long, List<BaseCategoryView>> groupedByCategory2Id = value.stream()
                    .collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));

            // 判空
            if (CollectionUtil.isNotEmpty(groupedByCategory2Id)) {
                for (Map.Entry<Long, List<BaseCategoryView>> entry1 : groupedByCategory2Id.entrySet()) {
                    CategoryList categoryList2 = new CategoryList();
                    Long category2Id = entry1.getKey();
                    String category2Name = baseCategory2Mapper.selectById(category2Id).getName();
                    List<BaseCategoryView> value1 = entry1.getValue();
                    List<CategoryList> categoryList3 = new ArrayList<>();
                    // 处理三级分类，三级分类没有子分类，所以不需要再分组
                    // 判空
                    if (CollectionUtil.isNotEmpty(value1)) {
                        for (BaseCategoryView baseCategoryView : value1) {
                            CategoryList categoryList31 = new CategoryList();
                            categoryList31.setCategoryId(baseCategoryView.getCategory3Id());
                            categoryList31.setCategoryName(baseCategory3Mapper.selectById(baseCategoryView.getCategory3Id()).getName());
                            categoryList3.add(categoryList31);
                        }
                    }
                    categoryList2.setCategoryId(category2Id);
                    categoryList2.setCategoryName(category2Name);
                    categoryList2.setCategoryChild(categoryList3);
                    categoryList1.add(categoryList2);
                }
            }
            categoryList.setCategoryId(category1Id);
            categoryList.setCategoryName(category1Name);
            categoryList.setCategoryChild(categoryList1);
            categoryLists.add(categoryList);
        }
        return categoryLists;
    }

    /**
     * 根据一级分类Id获取分类（标签名包含标签值） 列表
     * @param category1Id 1级分类ID
     * @return
     */
    @Override
    public List<BaseAttribute> getAttributesByCategory1Id(Long category1Id) {

//        // 方法一 使用代码完成查询
//        // 1、查询一级分类下的所有属性
//        List<BaseAttribute> baseAttributes = baseAttributeMapper.selectList(new LambdaQueryWrapper<BaseAttribute>().eq(BaseAttribute::getCategory1Id, category1Id));
//        // 遍历属性，查询属性值
//        for (BaseAttribute baseAttribute : baseAttributes) {
//            List<BaseAttributeValue> attributeValueList = baseAttributeValueMapper.selectList(new LambdaQueryWrapper<BaseAttributeValue>().eq(BaseAttributeValue::getAttributeId, baseAttribute.getId()));
//            baseAttribute.setAttributeValueList(attributeValueList);
//        }
//        return baseAttributes;

        // 方法二 使用sql完成查询
        return baseAttributeMapper.getAttributesByCategory1Id(category1Id);
    }
}
